package cn.juque.lucenecandy.core.datasync.listener.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.juque.lucenecandy.bo.IndexUpdateParamBO;
import cn.juque.lucenecandy.core.datasync.listener.ICommitListener;
import cn.juque.lucenecandy.core.enums.DocSyncTypeEnum;
import cn.juque.lucenecandy.vo.IndexUpdateParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>NullParamCommitListener</li>
 * </ul>
 * @date 2024-01-09
 **/
@Slf4j
@Service("nullParamCommitListener")
@Order(-999)
public class NullParamCommitListener implements ICommitListener {

    /**
     * 事前操作
     *
     * @param param 参数
     * @return boolean，false将会取消提交
     */
    @Override
    public Boolean before(IndexUpdateParamBO param) {
        boolean flag = Objects.nonNull(param)
                && CharSequenceUtil.isNotEmpty(param.getIndexName())
                && CharSequenceUtil.isNotEmpty(param.getClassName())
                && Objects.nonNull(param.getSyncType());
        if (!flag) {
            return false;
        }
        if ((DocSyncTypeEnum.ADD.equals(param.getSyncType()) || DocSyncTypeEnum.UPDATE.equals(param.getSyncType()))) {
            flag = CollUtil.isNotEmpty(param.getContent());
        }
        if (DocSyncTypeEnum.DEL.equals(param.getSyncType())) {
            flag = CollUtil.isNotEmpty(param.getIdList());
        }
        return flag;
    }

    /**
     * 事后操作
     *
     * @param param 参数
     */
    @Override
    public void after(IndexUpdateParamBO param) {
        // do nothing
    }
}
